package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IDocumentRecordService;
import com.mdd.admin.validate.DocumentRecordCreateValidate;
import com.mdd.admin.validate.DocumentRecordUpdateValidate;
import com.mdd.admin.validate.DocumentRecordSearchValidate;
import com.mdd.admin.vo.DocumentRecordListedVo;
import com.mdd.admin.vo.DocumentRecordDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.DocumentRecord;
import com.mdd.common.mapper.DocumentRecordMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 发文记录实现类
 * @author LikeAdmin
 */
@Service
public class DocumentRecordServiceImpl implements IDocumentRecordService {
        
    @Resource
    DocumentRecordMapper documentRecordMapper;

    /**
     * 发文记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<DocumentRecordListedVo>
     */
    @Override
    public PageResult<DocumentRecordListedVo> list(PageValidate pageValidate, DocumentRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DocumentRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        documentRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:recipients:str",
            "eq:theme:str",
            "like:customerName@customer_name:str",
        });

        IPage<DocumentRecord> iPage = documentRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DocumentRecordListedVo> list = new LinkedList<>();
        for(DocumentRecord item : iPage.getRecords()) {
            DocumentRecordListedVo vo = new DocumentRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(TimeUtils.dateToTimestamp(item.getCreateTime())));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 发文记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return DocumentRecord
     */
    @Override
    public DocumentRecordDetailVo detail(Integer id) {
        DocumentRecord model = documentRecordMapper.selectOne(
                new QueryWrapper<DocumentRecord>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        DocumentRecordDetailVo vo = new DocumentRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 发文记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(DocumentRecordCreateValidate createValidate) {
        DocumentRecord model = new DocumentRecord();
        model.setRecipientsId(createValidate.getRecipientsId());
        model.setRecipients(createValidate.getRecipients());
        model.setTheme(createValidate.getTheme());
        model.setDocumentTypeId(createValidate.getDocumentTypeId());
        model.setDocumentType(createValidate.getDocumentType());
        model.setCustomerName(createValidate.getCustomerName());
        model.setDocumentTime(createValidate.getDocumentTime());
        model.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        model.setDocumentTemplate(createValidate.getDocumentTemplate());
        model.setMailLabel(createValidate.getMailLabel());
        model.setAchieveMatter(createValidate.getAchieveMatter());
        model.setAccessory(createValidate.getAccessory());
        model.setAddresser(createValidate.getAddresser());
        model.setCopy(createValidate.getCopy());
        model.setBlindCarbonCopy(createValidate.getBlindCarbonCopy());
        model.setMailDescribe(createValidate.getMailDescribe());
        model.setText(createValidate.getText());
        model.setMailSignature(createValidate.getMailSignature());
        model.setReturnableDate(createValidate.getReturnableDate());
        documentRecordMapper.insert(model);
    }

    /**
     * 发文记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(DocumentRecordUpdateValidate updateValidate) {
        DocumentRecord model = documentRecordMapper.selectOne(
                new QueryWrapper<DocumentRecord>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setRecipientsId(updateValidate.getRecipientsId());
        model.setRecipients(updateValidate.getRecipients());
        model.setTheme(updateValidate.getTheme());
        model.setDocumentTypeId(updateValidate.getDocumentTypeId());
        model.setDocumentType(updateValidate.getDocumentType());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setDocumentTime(updateValidate.getDocumentTime());
        model.setDocumentTemplate(updateValidate.getDocumentTemplate());
        model.setMailLabel(updateValidate.getMailLabel());
        model.setAchieveMatter(updateValidate.getAchieveMatter());
        model.setAccessory(updateValidate.getAccessory());
        model.setAddresser(updateValidate.getAddresser());
        model.setCopy(updateValidate.getCopy());
        model.setBlindCarbonCopy(updateValidate.getBlindCarbonCopy());
        model.setMailDescribe(updateValidate.getMailDescribe());
        model.setText(updateValidate.getText());
        model.setMailSignature(updateValidate.getMailSignature());
        model.setReturnableDate(updateValidate.getReturnableDate());
        documentRecordMapper.updateById(model);
    }

    /**
     * 发文记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        DocumentRecord model = documentRecordMapper.selectOne(
                new QueryWrapper<DocumentRecord>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        documentRecordMapper.updateById(model);
    }

}
